//
//  FriendsListTableViewController.m
//  group14
//
//  Created by lanou3g on 15/11/11.
//  Copyright © 2015年 吕志伟. All rights reserved.
//

#import "FriendsListTableViewController.h"
#import "FriendsListTableViewCell.h"
#import "FriendsModel.h"
#import "ChineseToPinyin.h"
#import "FriendsSecondTableViewController.h"


@interface FriendsListTableViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic, strong) NSMutableArray *LetterArray;
@property (nonatomic, strong) NSMutableArray *FriendsArray;
@property (nonatomic, strong) NSMutableArray *SectionArray;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSMutableArray *array1;

@property (nonatomic, strong) UISearchBar *SearchBar;



@end

@implementation FriendsListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    _LetterArray = [[NSMutableArray alloc]initWithObjects: @"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
    _FriendsArray = [[NSMutableArray alloc]initWithObjects:@"阿毛",@"毛驴",@"猴子",@"于继龙",@"王宝强",@"洋洋",@"熊二",@"任义春",@"文玄珠",@"朝日",@"呈登辉",@"佩总",@"马文强",@"老魏",@"蟠龙",@"杨磊",@"杨雅洁",@"皮城女警", @"赏金猎人", @"发条魔灵", @"放逐之刃", @"德邦总管", @"蛮族之王", @"暗夜猎手", @"大盖伦", @"伊泽瑞尔", @"刀锋意志", @"虚空恐惧", @"提莫队长", @"皮城执法官", @"蒸汽机器人", @"魂锁典狱长",@"熔岩巨兽" , @"影流之主", @"法外狂徒", @"麦林炮手", @"暗黑元首", @"光辉女郎", @"德玛西亚皇子", @"盲僧", @"疾风剑豪", @"暗影之拳", @"不祥之刃", @"牛头酋长", @"曙光女神", @"阿狸", @"琴瑟仙女", @"无极剑圣", @"荒漠死神", @"暴走萝莉", @"雷霆咆哮",@"sjf",@"nasa",nil];
    //    _FriendsArray = [[NSMutableArray alloc]initWithObjects:@"abc",@"ij",@"kl",@"as",@"lo",@"nm",@"vc", nil];
    _SectionArray = [NSMutableArray array];
    _array = [NSMutableArray array];
    _array1 = [NSMutableArray array];
    
    [self addmodel];
    
    [self.tableView reloadData];
}

- (void)addmodel
{
    for (int i = 0; i <_LetterArray.count; i++) {
        
        NSMutableArray *arr = [NSMutableArray array];
        NSString *letter = [_LetterArray objectAtIndex:i];
        for (NSString *s1 in _FriendsArray) {
            //第三方 汉字转字符
            char a = [ChineseToPinyin sortSectionTitle:s1];
            NSString* strr = [NSString stringWithFormat:@"%c",a];
            
            if ([letter isEqualToString:strr]) {
                [arr addObject:s1];
                
            }
            
        }
        if (arr.count != 0) {
            [_SectionArray addObject:arr];
            NSString *str = [arr firstObject];
            char b = [ChineseToPinyin sortSectionTitle:str];
            NSString *string = [NSString stringWithFormat:@"%c",b];
            [_array addObject:string];
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//开始搜索
//-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
//{
//    NSLog(@"开始搜索:%@",searchBar.text);
//}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    //NSLog(@"%s",__FUNCTION__);
    
    //按钮移动化的方式消失、：
    
    [searchBar  setShowsCancelButton:NO animated:YES];
    
    
    [searchBar resignFirstResponder];
    
    
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    for (id obj in [searchBar subviews]) {
        if ([obj isKindOfClass:[UIView class]]) {
            for (id obj2 in [obj subviews]) {
                if ([obj2 isKindOfClass:[UIButton class]]) {
                    UIButton *btn = (UIButton *)obj2;
                    [btn setTitle:@"取消" forState:UIControlStateNormal];
                }
            }
        }
    }
    //移动化的方式进行显示取消按钮
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains %@",searchText];
    [_SectionArray removeAllObjects];
    [_array removeAllObjects];
    
    if ([self.FriendsArray filteredArrayUsingPredicate:predicate].count > 0 && ![searchText isEqualToString:@""]) {
        NSMutableArray *array2 = [NSMutableArray array];
        [array2 addObjectsFromArray:[self.FriendsArray filteredArrayUsingPredicate:predicate]];
        if (array2.count != 0) {
            [_SectionArray addObject:array2];
            NSString *str = [array2 firstObject];
            char b = [ChineseToPinyin sortSectionTitle:str];
            NSString *string = [NSString stringWithFormat:@"%c",b];
            [_array addObject:string];
        }
    } else {
        
        [self addmodel];
    }
    [self.tableView reloadData];
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _SectionArray.count + 1;
}

//返回分区cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }else
    {
        
        NSMutableArray *array3 = [_SectionArray objectAtIndex:section - 1];
        return array3.count;
    }
}
//分区头部高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 50;
    }else
    {
        return 30;
    }
}

//返回分区头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        _SearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];
        _SearchBar.delegate = self;;
        return _SearchBar;
    }else
    {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        label.backgroundColor = [UIColor lightGrayColor];
        label.alpha = 0.3;
        label.text = [_array objectAtIndex:section - 1];
        return label;
    }
}
//返回分区名字
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _array;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FriendsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        NSMutableArray *array1 = [[NSMutableArray alloc]initWithObjects:@"新的朋友",@"群聊" ,nil];
        cell.myLabel.text = [array1 objectAtIndex:indexPath.row];
        return cell;
    }
    else
    {
        NSMutableArray *arr = _SectionArray[indexPath.section - 1];
        cell.myLabel.text = [arr objectAtIndex:indexPath.row];
        return cell;
    }
    
    
    
}

// 返回每个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView endEditing:YES];
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            NSLog(@"添加好友");
        }
        else
        {
            NSLog(@"群聊");
        }
    }
    else
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        FriendsSecondTableViewController *second = [mainStoryboard instantiateViewControllerWithIdentifier:@"SecondVC"];
        
        [self.navigationController pushViewController:second animated:YES];
    }
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
